package ren.kane.asdppp.transaction.pay;

import ren.kane.asdppp.PayrollDatabase;
import ren.kane.asdppp.Employee;
import ren.kane.asdppp.transaction.Transaction;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author renrk(1275) on 2019-04-21 9:58 AM
 **/
public class PaydayTransaction implements Transaction {

    private LocalDate payDate;

    private Map<Integer, Paycheck> paycheckMap = new HashMap<>();

    public PaydayTransaction() {
    }

    public PaydayTransaction(LocalDate payDate) {
        this.payDate = payDate;
    }

    @Override
    public void execute() {
        List<Integer> empIds = PayrollDatabase.getAllEmployeeIds();
        for (int empId : empIds) {
            Employee employee = PayrollDatabase.getEmployee(empId);
            if (employee.isPayDate(payDate)) {
                Paycheck paycheck = new Paycheck(employee.getPayPeriodStartDate(payDate), payDate);
                paycheckMap.put(empId, paycheck);
                employee.payday(paycheck);
            }
        }
    }

    public Paycheck getPaycheck(int empId) {
        return paycheckMap.get(empId);
    }

    public Map<Integer, Paycheck> getPaycheckMap() {
        return paycheckMap;
    }
}
